package ge.gen.eBdgt.db;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class DBConn {

	Connection conn;
	String dbName;

	public DBConn(String dbName) throws Exception {
		getConnection(dbName);
	}

	public Connection getConnection() throws Exception {
		Driver d = (Driver) Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")
				.newInstance();
		conn = DriverManager
				.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="
						+ dbName);
		return conn;

	}

	boolean checkConnection() {
		if (conn != null)
			return true;
		return false;
	}

	public Connection getConnection(String dbName) throws Exception {
		if (checkConnection())
			return conn;
		else {
			this.dbName = dbName;
			return getConnection();
		}
	}

	public void close() {
		if (checkConnection()) {
			conn = null;
		}
	}

	public Statement getStatement() throws SQLException {
		return conn.createStatement();
	}

	public void setAutoCommit(boolean b) throws SQLException {
		conn.setAutoCommit(b);
	}

	public void commit() throws SQLException {
		conn.commit();
	}

	public DatabaseMetaData getMetaData() throws SQLException {
		return conn.getMetaData();
	}
}